import mainTitle from '@/components/mainTilte/mainTilte'
import formOperate from '@/layout/mixin/formOperate'
import dicTransform from '@/components/dicTransform/index'
// import { downloadxlsx } from '@/utils/index'
const reportOrder = {
  data() {
    return {
      formVisibleDown: false,
      orderTypeDic: [
        {
          code: 0,
          name: '销售订单'
        },
        {
          code: 2,
          name: '借卖订单'
        },
        {
          code: 3,
          name: '客户订单'
        },
        {
          code: 4,
          name: '预售订单'
        },
        {
          code: 5,
          name: '亚马逊平台发货(FBA)'
        }
      ],
      date: [],
      queryParam: {
        page: 1,
        limit: 10
      },
      salesNumber: '0',
      salesAmount: '0',
      exportLoading: false,
      downLoadParams: {}
    }
  },
  mixins: [formOperate],
  components: {
    mainTitle,
    dicTransform
  },
  methods: {
    handleExport() {
      const vm = this
      //  vm.formVisibleDown = true
      const params = {}
      const object = vm.queryParam
      for (const key in object) {
        if (Object.hasOwnProperty.call(object, key)) {
          if (!['page', 'limit'].includes(key)) params[key] = object[key]
        }
      }
      vm.downLoadParams = params
      vm.formVisibleDown = true
      //   vm.exportLoading = true
      //   vm.$http.get(vm.exportUrl, { params }).then(res => {
      //     downloadxlsx(res.data.url)
      //     vm.exportLoading = false
      //   }).catch(() => {
      //     vm.exportLoading = false
      //   })
    },
    // 刷新
    handleRefresh() {
      const vm = this
      vm.queryParam.page = 1
      vm.queryParam.limit = 10
      vm.handleRefreshParams()
      vm.date = []
      vm.getList()
    },
    // 刷新参数
    handleRefreshParams() {
      Object.keys(this.queryParam).forEach(k => {
        // 如果未设置默认搜索参数，刷新将会清空搜索
        if (this.defaultQueryParam === undefined) {
          this.queryParam[k] = ''
        } else {
          // 清空不包含默认搜索参数
          if (this.defaultQueryParam.indexOf(k) === -1) {
            this.queryParam[k] = ''
          }
        }
      })
    },
    // 获取列表
    getList(params = null) {
      this.loading = true
      if (this.beforeSearch !== undefined) {
        this.beforeSearch()
      }
      let query = params || this.queryParam
      query = Object.assign({}, query)
      if (this.date?.length > 0) {
        const [start_at, end_at] = this.date
        query['start_at'] = start_at
        query['end_at'] = end_at
      }
      this.$http
        .get(this.url, { params: query })
        .then(response => {
          this.data = response.data.map(e => {
            e['shippingCodeList'] = e.shipping_code
              ? e.shipping_code.split(',')
              : []
            e['shippingCompanyList'] = e.shipping_company
              ? e.shipping_company.split(',')
              : []
            return e
          })
          this.salesNumber = response.salesNumber ?? 0
          this.salesAmount = response.salesAmount ?? 0
          this.handlePaginateResponse(response)
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    }
  }
}

export default reportOrder
